//

//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/1/31.
//

#import "RHHomePlantTopoCell.h"
#import "RHHomePlantTopoItemCell.h"
#import "RHPlantModel.h"
#import "RHTopoModel.h"
#import "StyledPageControl.h"

@interface RHHomePlantTopoCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate>

@property (nonatomic, strong) StyledPageControl *pageCtl;

@property (nonatomic, strong) NSMutableArray *statusViewArr;

@property (nonatomic, strong) UICollectionView *collectionView;


@end
@implementation RHHomePlantTopoCell
-(NSMutableArray *)statusViewArr
{
    if(!_statusViewArr){
        _statusViewArr = [NSMutableArray array];
    }
    return _statusViewArr;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
    }
    return self;
}

-(void)hidePageControl
{
    self.pageCtl.hidden = YES;
}

-(void)setPlantArr:(NSArray *)plantArr{
    _plantArr = plantArr;
    self.pageCtl.numberOfPages = plantArr.count;
    self.pageCtl.hidden = plantArr.count <2;
    [self.collectionView reloadData];
}
-(void)layoutSubviews
{
    NSLog(@"----------->111111111111111111");
}

-(void)setupUI
{
    kWeakSelf;
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    
    UIView *bgView = kView;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    bgView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
        make.height.offset(300);
    }];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection =  UICollectionViewScrollDirectionHorizontal;
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    [_collectionView registerClass:[RHHomePlantTopoItemCell class] forCellWithReuseIdentifier:PEHomeTopoHEAioCell];
    [_collectionView registerClass:[RHHomePlantTopoItemCell class] forCellWithReuseIdentifier:PEHomeTopoMECell];
    _collectionView.backgroundColor = [UIColor clearColor];
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = YES;
    [bgView addSubview:_collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    StyledPageControl *pageCtl = [[StyledPageControl alloc]init];
    pageCtl.pageControlStyle = PageControlStyleThumb;
    pageCtl.numberOfPages = 1;
    pageCtl.currentPage = 0;
    pageCtl.thumbImage = [UIImage imageNamed:@"kwt_pagectl_thum"];
    pageCtl.selectedThumbImage = [UIImage imageNamed:@"kwt_pagectl_selected"];
    [bgView addSubview:pageCtl];
    self.pageCtl = pageCtl;
    [pageCtl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(-5);
        make.centerX.mas_offset(0);
        make.width.offset(200);
        make.height.offset(15);
    }];
    [pageCtl addTarget:self action:@selector(pageDidChage:) forControlEvents:UIControlEventValueChanged];
    
    
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if(self.plantArr.count == 0)return 1;
    return self.plantArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RHHomePlantTopoItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:PEHomeTopoHEAioCell forIndexPath:indexPath];
    if(indexPath.item< self.plantArr.count){
        RHPlantModel *plantModel = self.plantArr[indexPath.item];
        cell.fullScreenAction = self.fullScreenAction;
        cell.productType = plantModel.productTypeId;
        cell.plantModel = plantModel;
    }
    if (self.plantArr.count == 0) {
        cell.plnatNameLab.text = nil;
    }
    
    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //有偶然的概率会出现非常大的尺寸,有可能是进入全屏的时候,这页面正在加载,取了横屏的宽度
    CGFloat width = MIN(kUI_WINDOW_W, kUI_WINDOW_H);
    return CGSizeMake(width , 300);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.clickAction) {
        self.clickAction(indexPath.item);
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{

    if(scrollView.isDragging){
        CGFloat pageWidth = kUI_WINDOW_W;
        NSInteger index;
        CGFloat x = scrollView.contentOffset.x + pageWidth*0.5;
        index = x /pageWidth;
        self.pageCtl.currentPage = index;
    }
}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    NSLog(@"--->手指已经松开,继续向前? %ld",decelerate);
    
    if([scrollView isEqual:self.collectionView]&&decelerate==NO){
        CGFloat pageWidth = kUI_WINDOW_W ;
        NSInteger index;
        CGFloat x = scrollView.contentOffset.x + pageWidth*0.5;
        index = x /pageWidth;
        self.pageCtl.currentPage = index;
        
    }
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"%s",__func__);
    if([scrollView isEqual:self.collectionView]){
        CGFloat pageWidth = kUI_WINDOW_W ;
        NSInteger index;
        CGFloat x = scrollView.contentOffset.x + pageWidth*0.5;
        index = x /pageWidth;
        self.pageCtl.currentPage = index;
       
    }
}



-(void)pageDidChage:(UIPageControl*)pageCtl
{
    CGFloat pageWidth = kUI_WINDOW_W ;
    [self.collectionView setContentOffset:CGPointMake(pageWidth * pageCtl.currentPage, 0) animated:YES];
   
    NSLog(@"---->index:%zd",pageCtl.currentPage);
}



@end
